﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using XISoft.AppCreator.Util.ICodeCreator;
using System.Collections;
using System.Text.RegularExpressions;
using XISoft.AppCreator.Util.Common;

namespace XISoft.AppCreator.Util.CompileHelper
{
    public static class CompileCommon
    {
        public const String ClassName = "className";
        public const String PropertyList = "varlist";

        public static Object GetProperty(Object target, String name)
        {
            return Property(target, name, null);
        }
        
        public static Object GetFlagValue(Hashtable vars, String str)
        {
            const String strPattern = @"(?<var>.*)\.(?<med>.*)$";
            MatchCollection Matches = Regex.Matches(str, strPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);

            if (Matches.Count == 0)
                return vars[str];

            Match match=Matches[0];
            return Property(GetFlagValue(vars, match.Groups["var"].Value), match.Groups["med"].Value, null);
        }

        public static String UpFirst(String source)
        {
            return source.Substring(0, 1).ToUpper() + source.Substring(1, source.Length - 1);
        }
        
        public static String LowFirst(String source)
        {
            return source.Substring(0, 1).ToLower() + source.Substring(1, source.Length - 1);
        }

        /*
        private static Object Invoke(Object target, String methodName, params Object[] param)
        {
            MethodInfo method = null;
            try
            {
                method = target.GetType().GetMethod(methodName);
                return method.Invoke(target, param);
            }
            catch (NullReferenceException e)
            {
                Logger.WriteError(typeof(CompileCommon).ToString(), e.Message);
                throw e;
            }
            catch (ArgumentNullException e)
            {
                Logger.WriteError(typeof(CompileCommon).ToString(), e.Message);
                throw e;
            }
            catch (ArgumentException e)
            {
                Logger.WriteError(typeof(CompileCommon).ToString(), e.Message);
                throw e;
            }
            catch (TargetParameterCountException e)
            {
                Logger.WriteError(typeof(CompileCommon).ToString(), e.Message);
                throw e;
            }
            return null;

        }*/


        private static Object Property(Object target, String methodName, params Object[] param)
        {
            PropertyInfo[] property = null;
            try
            {
                property = target.GetType().GetProperties();
                foreach (PropertyInfo p in property)
                    if (p.Name.ToUpper() == methodName.ToUpper())
                        return p.GetValue(target, param);
                throw new Exception("Attribute No Found!!!!!!!!");
            }
            catch (NullReferenceException e)
            {
                LogHelper.Trace(e);
                throw e;
            }
            catch (ArgumentNullException e)
            {
                LogHelper.Trace(e);
                throw e;
            }
            catch (ArgumentException e)
            {
                LogHelper.Trace(e);
                throw e;
            }
            catch (TargetParameterCountException e)
            {
                LogHelper.Trace(e);
                throw e;
            }
            catch (Exception e)
            {
                LogHelper.Trace(e);
                throw e;
            }
        }
    }
}
